package com.tripwego.client.ui.pageVoyage;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.tripwego.client.event.AddTripEvent;
import com.tripwego.client.event.AddTripEventHandler;
import com.tripwego.client.ui.pageAccueil.FormCreateTripWidget;
import com.tripwego.client.ui.pageVoyage.cr.CRTitleCloseWidget;
import com.tripwego.client.ui.pageVoyage.cr.CRWidget;
import com.tripwego.client.ui.pageVoyage.top.TopWidget;
import com.tripwego.client.ui.widget.InfosWidget;
import com.tripwego.client.ui.widget.MapWidget;

public class VoyageViewImpl extends Composite implements IVoyageView, AddTripEventHandler {

	private Presenter presenter;

	private EventBus eventBus;

	private VerticalPanel viewPanel = new VerticalPanel();

	private TopWidget topWidget = new TopWidget();

	private HorizontalPanel middlePanel = new HorizontalPanel();

	private MapWidget mapWidget = new MapWidget();

//	private FormCreateTripWidget formCreateTripWidget = new FormCreateTripWidget();
//	private DialogBox dialogBox_addTrip = null;

	private CRWidget cRWidget = new CRWidget();

	private Button button_test = new Button("<nobr>Test</nobr>");

	private final VerticalPanel verticalPanel = new VerticalPanel();
	private final HorizontalPanel horizontalPanel = new HorizontalPanel();

	private final CRTitleCloseWidget crTitleCloseWidget = CRTitleCloseWidget.getInstance();

	private InfosWidget infosWidget = new InfosWidget("Creez votre premiere etape");

	private final PropositionWidget propositionWidget = new PropositionWidget();

	/**
	 * 
	 */
	public VoyageViewImpl() {
		super();

		viewPanel.setStyleName("voyagePage");

		viewPanel.add(infosWidget);// position fixed

//		dialogBox_addTrip = (DialogBox) formCreateTripWidget.asWidget();

		viewPanel.setWidth("100%");
		viewPanel.setHeight("700px");

		middlePanel.setStyleName("middlePanel");
		middlePanel.setSpacing(5);

		middlePanel.add(crTitleCloseWidget.asWidget());
		middlePanel.add(cRWidget.asWidget());
		middlePanel.add(mapWidget);
		middlePanel.add(propositionWidget);

		middlePanel.setCellWidth(mapWidget, "100%");

		viewPanel.add(topWidget.asWidget());

		// topPanel.add(verticalPanel);

		verticalPanel.add(horizontalPanel);

		viewPanel.add(middlePanel);

		button_test.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

			}
		});

		crTitleCloseWidget.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				crTitleCloseWidget.hide();
				cRWidget.show();
			}
		});
	}

	@Override
	public Widget asWidget() {
		init();
		return viewPanel;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		this.topWidget.setPresenter(presenter);
//		this.formCreateTripWidget.setPresenter(presenter);
		this.cRWidget.setPresenter(presenter);
		this.mapWidget.setPresenter(presenter);
		this.infosWidget.setPresenter(presenter);
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
		this.topWidget.setEventBus(eventBus);
//		this.formCreateTripWidget.setEventBus(eventBus);
		this.cRWidget.setEventBus(eventBus);
		this.infosWidget.setEventBus(eventBus);

		// ecouteurs d'evenements
		eventBus.addHandler(AddTripEvent.TYPE, this);
	}

	@Override
	public void init() {
		crTitleCloseWidget.hide();

		cRWidget.init();

//		if (!name.isEmpty()) {
//			Long tripId = new Long(name);
//			presenter.initTrip(tripId);
//		} else {
////			dialogBox_addTrip.center();
////			dialogBox_addTrip.show();
////			formCreateTripWidget.init();
//		}
		
		topWidget.init();
	}

	@Override
	public void handleAddTripEvent(AddTripEvent event) {
		// button_addStep.setEnabled(true);
		// button_addActivite.setEnabled(true);
	}
}
